package com.xcjh.opencv.listen;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import com.xcjh.opencv.FindSimilarImage;
import com.xcjh.utils.SystemUtil;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Objects;

/**
 * @author Administrator
 */
public class GlobalKeyListenerExample implements NativeKeyListener {

    /**
     * 退出系统的键
     */
    private static final int EXIT_KEY=NativeKeyEvent.VC_F11;
    /**
     * 退出规则的键
     */
    private static final int EXIT_RULE_KEY=NativeKeyEvent.VC_F8;
    /**
     * 输入请联系我吧.
     */
    private static final int CALL_KEY=NativeKeyEvent.VC_F7;


    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {
        // 键盘按键被按下
        int keyCode = e.getKeyCode();
//        System.out.println("Key 按下: " + keyCode);
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent e) {
        // 键盘按键被释放
        int keyCode = e.getKeyCode();
        if(Objects.equals(EXIT_KEY,keyCode)){
            System.out.println("系统检测到按键退出");
            System.exit(0);
        }
        if(Objects.equals(EXIT_RULE_KEY,keyCode)){
            System.out.println("F8规则"+(FindSimilarImage.go?"暂停":"启动"));
            FindSimilarImage.go=!FindSimilarImage.go;
        }
        if(Objects.equals(CALL_KEY,keyCode)){
            sendCallMe();
        }
//        System.out.println("Key 释放: " + keyCode);
    }

    @Override
    public void nativeKeyTyped(NativeKeyEvent e) {
        // 键盘按键被输出.F1~12 还有Shift等 功能键不会打印（按下并释放）
//        char keyChar = e.getKeyChar();
//        System.out.println("Key 按下并释放: " + keyChar);
    }

    /**
     * 发送请联系我吧
     */
    public void sendCallMe(){
        SystemUtil.writeC("加我微信18200158896 一起聊聊吧,互相认识一下");
        Robot robot = FindSimilarImage.robot;
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        // 等待一段时间以确保粘贴完成
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        // 释放Ctrl+V
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
    }

}